/*++

Copyright (c) 2007  Microsoft Corporation 

Filename: 

    OpsMgrConnection.cs

Abstract: 

    A form to take SCOM Server Name as input and validate. 
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Data.SqlClient;
using AddIn.Windows.Forms;
using Microsoft.EnterpriseManagement;
using System.Globalization;

namespace NRTMAddIn
{
    public partial class OpsMgrConnection : AddIn.Windows.Forms.InteriorWizardPage
    {
        #region Variables

        //Stores Connection Strings
        //private SqlConnection sqlConnOps;

        #endregion

        #region Methods
        /// <summary>
        /// Constructor
        /// </summary>
        public OpsMgrConnection()
        {
            InitializeComponent();
            this.txtBoxSCOMName.Enter += new EventHandler(txtBoxSCOMName_Enter);
            this.lblSCOMName.Text = Global.GetResourceString("SCOMRoot_Label");
            this.m_subtitleLabel.Text = Global.GetResourceString("SCOMConnectionSubTitle_Text");
            this.titleLabel.Text = Global.GetResourceString("SCOMConnectionTitle_Text");
            this.txtBoxSCOMName.Text = Global.ServerName;
        }

        void txtBoxSCOMName_Enter(object sender, EventArgs e)
        {
            this.txtBoxSCOMName.SelectAll();
        }

        /// <summary>
        /// On Form Active
        /// </summary>
        /// <returns></returns>
        protected override bool OnSetActive()
        {
            if (!base.OnSetActive())
                return false;

                // Enable both the Next and Back buttons on this page    
            Wizard.SetWizardButtons(WizardButtons.Back | WizardButtons.Next);
            return true;
        }

        
        /// <summary>
        /// On Next clicked
        /// </summary>
        /// <returns></returns>
        protected override string OnWizardNext()
        {
            bool userException = false;

            ManagementGroup mg = null;

            errorProvider1.Clear();

            try
            {
                this.FindForm().Cursor = Cursors.WaitCursor;

                if (string.IsNullOrEmpty(txtBoxSCOMName.Text.Trim()))
                {
                    userException = true;
                    throw new NrtmException(Global.GetResourceString("SCOMRoot_Blank_Text"));
                }

                mg = ManagementGroup.Connect(txtBoxSCOMName.Text.Trim());

                if (!mg.IsConnected)
                {
                    userException = true;

                    // SDK Service not started
                    throw new NrtmException(String.Format(CultureInfo.InvariantCulture, Global.GetResourceString("SDK_Error")
                        , txtBoxSCOMName.Text.Trim()));
                }

                Global.ServerName = txtBoxSCOMName.Text.Trim();

                return "MachineSelector";

            }
            catch (System.Configuration.ConfigurationException)
            {
                errorProvider1.SetError(txtBoxSCOMName, Global.GetResourceString("DotNetFrameWork3ErrorMsg"));
                return "OpsMgrConnection";
            }
            catch (Microsoft.EnterpriseManagement.Common.ServerDisconnectedException)
            {
                errorProvider1.SetError(txtBoxSCOMName, String.Format(CultureInfo.InvariantCulture, Global.GetResourceString("AccessErrorMsg"), System.Environment.UserDomainName, System.Environment.UserName));
                return "OpsMgrConnection";
            }
            catch (Microsoft.EnterpriseManagement.Common.ServiceNotRunningException ex)
            {
                errorProvider1.SetError(txtBoxSCOMName, ex.Message);
                return "OpsMgrConnection";
            }
            catch (Microsoft.EnterpriseManagement.Common.UnauthorizedAccessMonitoringException ex)
            {
                errorProvider1.SetError(txtBoxSCOMName, ex.Message);
                return "OpsMgrConnection";
            }
            catch (NrtmException ex)
            {
                if (userException)
                {
                    //User exception set to display error messages to user
                    userException = false;
                    errorProvider1.SetError(txtBoxSCOMName, ex.Message);
                    return "OpsMgrConnection";
                }
                //To catch and display exceptions from sub functions
                NrtmException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
                            Global.GetResourceString("Exception_UsrMsg"), ex.InnerException);
                return "OpsMgrConnection";
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains(Global.GetResourceString("Unknown_Host")))
                {
                    errorProvider1.SetError(txtBoxSCOMName, ex.Message);
                    return "OpsMgrConnection";
                }
                else
                {
                    //To catch and display exceptions from current functions
                    NrtmException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
                                Global.GetResourceString("Exception_UsrMsg"), ex);
                    return "OpsMgrConnection";
                }
            }
            finally
            {
                mg = null;

                this.FindForm().Focus();
                this.FindForm().Cursor = Cursors.Default;
            }

        }

  #endregion Methods

    }
}

